/*
 * Created on 2005-4-1
 * Copyright: Bluejay Networks Inc. 2004
 *
 * This software is the proprietary information of Bluejay Networks Inc. & 
 * Inquiry Management Systems Ltd.  
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;

import com.ims.iaa.AD.AdImage;

/**
 * @author changsheng gong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ModifyHeightAndWidth {

    public static void main(String[] args) {
        Properties pro = new Properties();

		try  {
			pro.load(new FileInputStream("security\\db.properties"));
		} catch (IOException e)  {
			IaaUtil.log("Can not open the database properties file");
		}
		
		String user = pro.getProperty("db.userName");
		String password = pro.getProperty("db.password");
		String driver = pro.getProperty("db.driverName");
		String url = pro.getProperty("db.url");

		try{
		    Class.forName(driver);     
            Connection cn = DriverManager.getConnection( url, user, password);
            Statement stmt = null ;
            ResultSet rs = null;
 
	        String sql = " SELECT tblImpression.intImpressionHeight, tblImpression.intImpressionWidth, " +
	        		     " tblImpression.intImpressionID, tblImpression.intImageID, tblImageData.imgImage " +
	        			 " FROM tblImpression INNER JOIN" +
	        			 " tblImageData ON " +
	        			 " tblImpression.intImageID = tblImageData.intImageID " +
	                     " WHERE (tblImpression.bitState = 1 ) AND (tblImpression.intImpressionHeight = 0 " +
	                     " OR tblImpression.intImpressionWidth = 0) AND " +
	                     " tblImpression.strAdvertisementType LIKE 'IMG%'";
            stmt = cn.createStatement();
	        rs = stmt.executeQuery(sql);
        
	        byte[] buf = new byte[1024];
	        AdImage image = new AdImage();
	        
	        Statement st = null ;
	        
	        cn.setAutoCommit(false);
	        
            System.out.println("-----------------------------------------------");
            System.out.println("Analyze begin:");
            System.out.println("-----------------------------------------------");
            
            HashMap hm = new HashMap();
            int count = 0;
            try{
		        while(rs.next()){
		            try{
		                int intImpressionHeigth = rs.getInt(1);
			            int intImpressionWidth = rs.getInt(2);		       
			            int intImpressionID = rs.getInt(3);
			            int intImageID = rs.getInt(4);
			            Integer iImageId = new Integer(intImageID);
			            if(hm.containsKey(iImageId) == false){
				            hm.put(iImageId , iImageId);
				            
				            buf = rs.getBytes(5);
				            
				            image.setData( buf );
				            image.prepareImageHeight();
							image.prepareImageWidth();
										            
							intImpressionHeigth = image.getHeight();
							intImpressionWidth = image.getWidth();
							if(intImpressionHeigth < 0){
					            image.setData( buf );
					            image.prepareImageHeight();
								image.prepareImageWidth();
											            
								intImpressionHeigth = image.getHeight();
								intImpressionWidth = image.getWidth();
								System.out.println(intImpressionHeigth);
							}
							System.out.println("\t--------------------------------------- " );
					        System.out.println("\tget new hight and width for Impression : " + intImpressionID);
					        System.out.println("\tThe Impression : " + intImpressionID + ", its heigth is " + intImpressionHeigth);
					        System.out.println("\tThe Impression : " + intImpressionID + ", its width is " + intImpressionWidth);
	                        
					            
							String sq = "UPDATE tblImpression "
							          + " SET tblImpression.intImpressionHeight= " + intImpressionHeigth +","
							          + " tblImpression.intImpressionWidth = " + intImpressionWidth
							          + " WHERE tblImpression.intImageID = " + intImageID ;
							st = cn.createStatement();
							st.executeUpdate(sq);
							
							sq = "UPDATE tblImage "
						          + " SET tblImage.intImageHeight= " + intImpressionHeigth +","
						          + " tblImage.intImageWidth = " + intImpressionWidth
						          + " WHERE tblImage.intImageID = " + intImageID ;
							st = cn.createStatement();
							st.executeUpdate(sq);
							
							cn.commit();
							st.close();
							
							System.out.println("\tUpdate db for the new hight and width of the Impression : " + intImpressionID);
							System.out.println("\t---------------------------------------- " );
			            }
	                   count++;
		            }catch(Exception e){
		                cn.rollback();
		                e.printStackTrace();
		            }
		            
		        }//while
            }finally{
                if(cn != null){
                    cn.close();
                }
            }
	        if(count == 0){
	            System.out.println("-----------------------------------------------");
	            System.out.println("There is no zero size of image in the db!");
	            System.out.println("");
	            System.out.println("Analyze end!");
	            System.out.println("-----------------------------------------------");    
	        }else{
		        System.out.println("-----------------------------------------------");
	            System.out.println("Analyze end:have changed " + count + " images' height and width");
	            System.out.println("-----------------------------------------------");
            }
		}catch(Exception ex){
		    ex.printStackTrace();
		}
    }
}
